package com.project.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.project.bean.ProcureApplyBean;
import com.project.bean.ProcureApplyInfoBean;
import com.project.bean.ProcureBean;
import com.project.dto.ApplyInfoDto;
import com.project.dto.ProcureApplyDto;
import com.project.service.IProcureApplyService;
import com.project.service.IProcureService;
import com.project.util.SnowIdUtils;
import com.project.vo.ProcureApplyVo;
import org.apache.commons.collections.list.PredicatedList;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 采购申请controller
 */
@RestController
@RequestMapping("procureApply")
public class ProcureApplyController {
    @Autowired
    private IProcureApplyService procureApplyService;
    @Autowired
    private IProcureService procureService;

    @Autowired
    private RabbitTemplate rabbitTemplate;
    @RequestMapping("addProcureApply")
        public Boolean addProcureApply(String reason){
        ProcureApplyBean procureApplyBean=new ProcureApplyBean();
        procureApplyBean.setProcureApplyId(SnowIdUtils.uniqueLongHex());
        procureApplyBean.setReason(reason);

        //添加申请
        procureApplyService.addProcureApply(procureApplyBean);

//        ApplyInfoDto applyInfoDto=new ApplyInfoDto();
//        applyInfoDto.setApplyRef(procureApplyBean.getProcureApplyId());//采购申请编号
//        applyInfoDto.setApplyNumber(procureApplyBean.);



        List<ProcureBean> procureBeanList=procureService.findAllProcureList();
//        String result = procureBeanList.stream()
//                .filter(procureBean -> procureBean.getNum() != 0)
//                .findFirst()
//                .map(Object::toString)
//                .orElse("默认字符串");
        Boolean result=true;

        for (int i=0;i<procureBeanList.size();i++){
            if (procureBeanList.get(i).getNum()==0){
                result =false;
                break;
            }
        }
        if (result==true){
            for (int i=0;i<procureBeanList.size();i++){
                ProcureBean procureBean=procureBeanList.get(i);
                ProcureApplyInfoBean procureApplyInfoBean=new ProcureApplyInfoBean();
                procureApplyInfoBean.setProcureApplyId(procureApplyBean.getProcureApplyId());
                procureApplyInfoBean.setNum(procureBean.getNum());//数量
                procureApplyInfoBean.setProductName(procureBean.getProductName());//商品名
                procureApplyInfoBean.setColor(procureBean.getColor());
                procureApplyInfoBean.setSize(procureBean.getSize());
                procureApplyService.addProcureApplyInfoOne(procureApplyInfoBean);//添加商品到采购申请详情表
                procureService.del(procureBean.getId());//删除原商品记录
            }
        }else {
            return result;
        }
        //添加详细详细
//        if (st=="ok"&&procureApplyBean.getProcureApplyInfoList()!=null){
//            procureApplyService.addProcureApplyInfo(procureApplyBean.getProcureApplyInfoList());
//        }


        return result;
    }
    @RequestMapping("sendProcureApplyMq")
    public String sendProcureApplyMq(String procureApplyId){

        ProcureApplyBean procureApplyBean=procureApplyService.findByProcureApplyId(procureApplyId);
        //set到dto，想mq发送
        ProcureApplyDto procureApplyDto=new ProcureApplyDto();
        procureApplyDto.setApplyRef(procureApplyBean.getProcureApplyId());//采购申请编号
        procureApplyDto.setDateTime(LocalDateTime.now());//采购申请时间
        procureApplyDto.setReason(procureApplyBean.getReason());//采购原因


        List<ApplyInfoDto>applyInfoDtos=new ArrayList<>();
        List<ProcureApplyInfoBean>list=procureApplyBean.getProcureApplyInfoList();

        list.forEach(procureApplyInfoBean -> {
            ApplyInfoDto applyInfoDto=new ApplyInfoDto();
            applyInfoDto.setApplyRef(procureApplyBean.getProcureApplyId());
            applyInfoDto.setApplyNumber(procureApplyInfoBean.getNum());
            applyInfoDto.setProductName(procureApplyInfoBean.getProductName());
            applyInfoDto.setSize(procureApplyInfoBean.getSize());
            applyInfoDto.setColor(procureApplyInfoBean.getColor());
            applyInfoDtos.add(applyInfoDto);
        });
        procureApplyDto.setApplyInfoEntityList(applyInfoDtos);//采购的商品
       String  s =  JSON.toJSONString(procureApplyDto);
        rabbitTemplate.convertAndSend("BackPurchaseApply","purchase.key",s);
        return "ok";
    }

//    @RequestMapping("re")
//    public String re(){
//        System.out.println("ok");
//        rabbitTemplate.convertAndSend("BackPurchaseApply","purchase.key","test");
//        return "ok";
//    }




    /**
     * 分页查询采购申请
     * @param page
     * @return
     */
    @RequestMapping("cutByItem")
    public IPage<ProcureApplyVo> cutByItem(int page){
        return procureApplyService.cutByItem(page);
    }

    /**
     * 通过采购申请编号查询采购申请详细信息及采购明细
     * @param procureApplyId 采购申请编号
     * @return 返回申请对象
     */
    @RequestMapping("findByProcureApplyId")
    public ProcureApplyBean findByProcureApplyId(String procureApplyId){
        return procureApplyService.findByProcureApplyId(procureApplyId);
    }

    /**
     * 修改采购商品的数量
     * @param productId 商品编号
     * @param newProductNum 新采购数量
     * @return  是否修改成功
     */
    @RequestMapping("updateProcureNum")
    String updateProcureNum(String productId,int newProductNum){
        return procureApplyService.updateProcureNum(productId, newProductNum);
    }
}
